package com.example.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.dao.StorageRepository;
import com.example.entity.Storage;

@Service
public class StorageService {
	@Resource
	private StorageRepository storageRepository;

	@Transactional
	public Storage reduceStorage(Integer goodsId, Integer quantity) {
		Storage storage = storageRepository.findById(goodsId).get();
		if (storage.getQuantity() < quantity) {
			throw new IllegalStateException(goodsId + "商品库存不足");
		}
		storage.setQuantity(storage.getQuantity() - quantity);
		return storageRepository.save(storage);
	}
}